{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "ck+_to_png.ipynb",
      "provenance": [],
      "mount_file_id": "https://github.com/amilkh/cs230-fer/blob/master/datasets/affectnet_to_png.ipynb",
      "authorship_tag": "ABX9TyP0yZ3hd0CQIaLfMbomzjaD",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/amilkh/cs230-fer/blob/master/datasets/affectnet_to_png.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PWC1w1-0xJlE",
        "colab_type": "code",
        "outputId": "bc030a88-0696-4088-b847-2cf038382011",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 128
        }
      },
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n",
            "\n",
            "Enter your authorization code:\n",
            "··········\n",
            "Mounted at /content/drive\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "J76fXHTw-FCU",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 72
        },
        "outputId": "5d3c3b27-4581-4b4c-d8ea-d3d42a7e5f1e"
      },
      "source": [
        "! rm -rf /content/affectnet && mkdir /content/affectnet\n",
        "! unrar e -idq '/content/drive/My Drive/cs230 project/dataset/affectnet/Manually_Annotated.part01.rar' /content/affectnet"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "\n",
            "Cannot find volume /content/drive/My Drive/cs230 project/dataset/affectnet/Manually_Annotated.part06.rar\n",
            "Manually_Annotated_Images/461/d45ace15e1ed59b21e7665fe337d056d7236bf1b668a0b64cdaeea12.jpg - checksum error"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "a3gZOJmkEpcX",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "3c4eb36e-e54b-4e75-902c-7f6a636b2492"
      },
      "source": [
        "! ls -1 /content/affectnet | wc -l"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "193591\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cJgl4RTOzaSu",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import pandas as pd\n",
        "\n",
        "train_csv = pd.read_csv('/content/drive/My Drive/cs230 project/dataset/affectnet/training.csv')\n",
        "\n",
        "# FER2013 = {0:'angry', 1:'disgust', 2:'fear', 3:'happy', 4:'sad', 5:'surprise', 6:'neutral'}\n",
        "# AffectNet = 0: Neutral, 1: Happiness, 2: Sadness, 3: Surprise, 4: Fear, 5: Disgust, 6: Anger, 7: Contempt, 8: None, 9: Uncertain, 10: No-Face\n",
        "def reindex_labels(y):\n",
        "    label_mapping = {0:6,1:3,2:4,3:5,4:2,5:1,6:0,7:7,8:8,9:9,10:10}\n",
        "    for label in label_mapping.keys():\n",
        "        y[y == label] = label_mapping[label]\n",
        "\n",
        "    return y"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bqSj7t1MFde5",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 98
        },
        "outputId": "2a700724-edd9-44be-d24d-450a196443f2"
      },
      "source": [
        "train_csv[train_csv['subDirectory_filePath']=='163/7b5d3644888582a32c7896e7f58f400f179f1fd8c48feb286e2cd8d4.jpg']"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>subDirectory_filePath</th>\n",
              "      <th>face_x</th>\n",
              "      <th>face_y</th>\n",
              "      <th>face_width</th>\n",
              "      <th>face_height</th>\n",
              "      <th>facial_landmarks</th>\n",
              "      <th>expression</th>\n",
              "      <th>valence</th>\n",
              "      <th>arousal</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>11</th>\n",
              "      <td>163/7b5d3644888582a32c7896e7f58f400f179f1fd8c4...</td>\n",
              "      <td>100</td>\n",
              "      <td>100</td>\n",
              "      <td>669</td>\n",
              "      <td>669</td>\n",
              "      <td>220.53;366.29;223.18;430.64;234.1;493.62;250.6...</td>\n",
              "      <td>10</td>\n",
              "      <td>-2.0</td>\n",
              "      <td>-2.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                                subDirectory_filePath  face_x  ...  valence  arousal\n",
              "11  163/7b5d3644888582a32c7896e7f58f400f179f1fd8c4...     100  ...     -2.0     -2.0\n",
              "\n",
              "[1 rows x 9 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zd3pBrfRWLCs",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "train_csv['subDirectory_filePath'] = train_csv['subDirectory_filePath'].apply(lambda fp: [fp.split('/')[1]])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Uu4HDJJtFukB",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "f7c53ecb-f659-4952-abd3-383456296355"
      },
      "source": [
        "train_csv(0)"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<pandas.core.indexing._iLocIndexer at 0x7f3bbf7adcc8>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3pk_IV0LaqPn",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from PIL import Image\n",
        "\n",
        "for i, row in train_csv.iterrows():\n",
        "  try:\n",
        "    fn=row['subDirectory_filePath'].split('/')[1][0]\n",
        "    img=Image.open('/content/affectnet/'+fn)\n",
        "  except IOError as e:\n",
        "    pass\n",
        "  else:\n",
        "    img.show()\n",
        "    break"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "REQj2eX6Y7ZJ",
        "colab_type": "code",
        "outputId": "d550066b-8d9d-459d-f007-b576f586e81e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "train_csv[train_csv['subDirectory_filePath'] == train_csv.iloc[0]['subDirectory_filePath'][0]]['subDirectory_filePath']"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Series([], Name: subDirectory_filePath, dtype: object)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 43
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BVwlKRu-9qSN",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from os import listdir\n",
        "for fn in listdir('/content/affectnet'):\n",
        "    if fn.endswith(\".jpg\"):\n",
        "      print(train_csv['subDirectory_filePath'==fn])\n",
        "      break\n",
        "    else:\n",
        "      continue"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bezzV2CF8c6o",
        "colab_type": "code",
        "outputId": "46ac3c6a-b68f-4b93-e4d3-b42dfb57a173",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "#e = 0\n",
        "#train_csv_e=train_csv[train_csv['expression']==e]\n",
        "\n"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(74874, 9)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7thKHuzr4enP",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np\n",
        "\n",
        "X = np.load('/content/ck-output/npy_files/X.npy')\n",
        "y8 = np.load('/content/ck-output/npy_files/y.npy')\n",
        "y = reindex_labels(y8)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GSH3CX8IE5yj",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import matplotlib.pyplot as plt\n",
        "\n",
        "emotions = {0:'angry', 1:'disgust', 2:'fear', 3:'happy', 4:'sad', 5:'surprise', 6:'neutral'}\n",
        "\n",
        "for i in range(0,10):\n",
        "  plt.xlabel(emotions[y[i]])\n",
        "  plt.imshow(X[i].reshape(96,96),cmap='gray')\n",
        "  plt.figure()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Rd-enGupIs_1",
        "colab_type": "code",
        "outputId": "362718e2-8d0d-423c-9595-8b3963f600d3",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "X.shape"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(1232, 1, 96, 96)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 70
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZvgocTG4KF1v",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from os import mkdir\n",
        "mkdir('/content/ck-images/')\n",
        "for emotion in emotions:\n",
        "  mkdir(f'/content/ck-images/' + f'{emotion} ' + f'{emotions[emotion]}')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ojFKvNRXFA5G",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from PIL import Image\n",
        "\n",
        "count = 0\n",
        "for i in range(0,X.shape[0]):\n",
        "    count_string = str(count).zfill(7)\n",
        "    fname = '/content/ck-images/' + f'{y[i]} ' + f'{emotions[y[i]]}/' + f'{emotions[y[i]]}-{count_string}.png'\n",
        "    img = Image.fromarray(X[i].reshape(96,96))\n",
        "    img=img.resize((48,48))\n",
        "    img.save(fname) \n",
        "    count += 1"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "b-lZOMv4JCPC",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "! cd ck-images && zip -r ck-plus.zip *"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pCsREeBHK2N7",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "! mv ck-images/ck-plus.zip '/content/drive/My Drive/cs230 project/dataset/'"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lZlxu8ciL0Y4",
        "colab_type": "code",
        "outputId": "d39db1b8-ab03-4606-c47b-377b7882939f",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 326
        }
      },
      "source": [
        "! ls -lh '/content/drive/My Drive/cs230 project/dataset/'"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "total 218M\n",
            "drwx------  2 root root 4.0K Mar 15 04:00  ck+\n",
            "-rw-------+ 1 root root 2.3M Mar 18 19:49  ck-plus.zip\n",
            "drwx------  2 root root 4.0K Mar 15 02:57  emotion\n",
            "-rw-------  1 root root  50M Mar 15 03:01  emotion.zip\n",
            "-rw-------  1 root root  51M Mar 15 18:29  facesdb.zip\n",
            "drwx------  2 root root 4.0K Mar 12 17:11  fer2013\n",
            "-rw-------  1 root root 299K Mar 15 15:53  googlesearch.zip\n",
            "drwx------  2 root root 4.0K Mar 15 03:15  googleset\n",
            "-rw-------  1 root root 3.8M Mar 15 03:15  googleset.zip\n",
            "drwx------  2 root root 4.0K Mar 12 21:21  jaffedbase\n",
            "-rw-------  1 root root  12M Mar 15 18:29  jaffe.zip\n",
            "drwx------  2 root root 4.0K Mar 15 15:32  old\n",
            "-rw-------  1 root root 100M Mar 15 15:32  umea.zip\n",
            "dr-x------  2 root root 4.0K Mar 12 17:11  webcam\n",
            "-rw-------  1 root root 588K Mar 18 00:04 'webcam amil.zip'\n",
            "-rw-------  1 root root 346K Mar 17 23:46 'webcam paki.zip'\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uAA7hlqTL-el",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}